﻿using System.Diagnostics.Contracts;

namespace Hippo.Common.Extensions
{
    using System;
    using System.Collections.Specialized;

    public static class NameValueCollectionExtensions
    {
        public static bool TryGetValue<T>(this NameValueCollection collection, string identifier, out T result)
        {
            if (collection == null)
            {
                throw new ArgumentNullException("collection");
            }
            if (string.IsNullOrWhiteSpace(identifier))
            {
                throw new ArgumentNullException("identifier", "The identifier parameter can't be null, empty or only whitespace");
            }
            Contract.EndContractBlock();

            result = default(T);
            if (collection[identifier] == null)
            {
                return false;
            }

            result = (T)Convert.ChangeType(collection[identifier], typeof(T));
            return true;
        }
    }
}
